/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package datasource;

import datasource.pixel.Pixel;
import datasource.pixel.RGB;
import datasource.util.BitWise;

/**
 * 
 * @author Fabian
 */
public class Imagen {

	private long headerIndex;
	private int bitspp;
	private int width;
	private int height;
	private byte[][] stripArray;
	private boolean littleEndian = true;

	/**
	 * Funcion que toma el stripArray de la misma clase y retorna su
	 * representacion como un arreglo matricial de objetos Pixel
	 * 
	 * @return
	 */
	public Pixel[][] createPixelMatrix() {
		Pixel[][] retorno = new Pixel[height][width];
		// littleendian
		if (littleEndian) {
			int k = 0;
			int j = 0;
			for (int i = 0; i < stripArray.length; i++) {
				byte[] currentStrip = stripArray[i];
				int tempIndex = 0;
				System.out.println("createPixelMatrix() " + i);
				while (k < height) {
					for (j = 0; j < width; j++) {
						if (tempIndex < stripArray[i].length) {
							int hexavalue = BitWise
									.byteArrayToIntLE(new byte[] {
											(currentStrip[tempIndex + 0]),
											(currentStrip[tempIndex + 1]),
											(currentStrip[tempIndex + 2]) });
							retorno[k][j] = new RGB(hexavalue);
							tempIndex += (bitspp / 8);
						}
					}
					k++;
				}
			}
		} else {
			int k = 0;
			int j = 0;

			for (int i = 0; i < stripArray.length; i++) {
				byte[] currentStrip = stripArray[i];
				// System.out.println("bytes por strip() " +
				// currentStrip.length);
				int tempIndex = 0;
				// System.out.println("createPixelMatrix() strip:" + i);
				while (tempIndex < stripArray[i].length) {
					// System.out.println("fila " + k);
					for (j = 0; j < width; j++) {
						// if (tempIndex < stripArray[i].length) {
						// System.out.println("Columna " + j);
						int hexavalue = BitWise.byteArrayToIntLE(new byte[] {
								(currentStrip[tempIndex + 2]),
								(currentStrip[tempIndex + 1]),
								(currentStrip[tempIndex + 0]) });
						retorno[k][j] = new RGB(hexavalue);
						tempIndex += (bitspp / 8);
						// }else{
						// System.out.println("tempIndex fuera de rango "+tempIndex);
						// break;
						// }
					}
					k++;
				}
			}
		}
		return retorno;
	}

	public Imagen() {

	}

	public long getHeaderIndex() {
		return headerIndex;
	}

	public void setHeaderIndex(long headerIndex) {
		this.headerIndex = headerIndex;
	}

	public int getBitspp() {
		return bitspp;
	}

	public void setBitspp(int bitspp) {
		this.bitspp = bitspp;
	}

	public int getWidth() {
		return width;
	}

	public void setWidth(int width) {
		this.width = width;
	}

	public int getHeight() {
		return height;
	}

	public void setHeight(int height) {
		this.height = height;
	}

	public void setStripArray(byte[][] stripArray) {
		this.stripArray = stripArray;
	}

	public byte[][] getStripArray() {
		return stripArray;
	}

	public void setStripArrayAt(byte[] row, int i) {

		if (stripArray == null) {
			stripArray = new byte[1][];
		}
		if (stripArray[i] != null) {
			System.out.println("**");
			stripArray[i] = row;
		} else {
			System.out.println("__");
			stripArray[0] = row;
		}

	}

	public boolean isLittleEndian() {
		return littleEndian;
	}

	public void setLittleEndian(boolean littleEndian) {
		this.littleEndian = littleEndian;
	}
}
